import { useCallback, useState } from "react"

export default (asyncFunction) => {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);

  const execute = useCallback(() => {
    setData(null);
    setLoading(true);
    setError(null);
    console.log("1")

    return asyncFunction()
      .then((response) => {
        console.log("2")
        setData(response);
        setLoading(false);
      })
      .catch((error) => {
        console.log("3")
        setError(error);
        setLoading(false);
      })
  }, [asyncFunction])
  return { data, loading, error, execute }
}